package com.atfcm.stack;

/**
 * @Author 96534
 * @create 2025/3/21 11:10
 */
public class T_20_有效括号 {
    // ([)]  {{{}}}  ()[]   【（）】

    public boolean isValid(String s) {
        StringBuilder stack = new StringBuilder();
        int top = -1;
        for(int i = 0; i < s.length(); i++){
            char c = s.charAt(i);
            if(top > -1  &&  isPaird(stack.charAt(top), c)){
                stack.deleteCharAt(top);
                top--;
            }else {
                top++;
                stack.append(c);
            }
        }
        return stack.length() == 0;
    }

    public boolean isPaird(char stockTop, char newchar) {
        if(stockTop == '(' && newchar == ')'){
            return true;
        }
        if(stockTop == '{' && newchar == '}'){
            return true;
        }
        if(stockTop == '[' && newchar == ']'){
            return true;
        }
        return false;
    }
}
